import 'dart:async';

import 'package:lepig/common/CategoryEvent.dart';
import 'package:lepig/common/EventBus.dart';
import 'package:lepig/model/SearchModel.dart';
import 'package:lepig/model/SelectProductTypeModel.dart';
import 'package:lepig/request/HttpSelectProductType.dart';
import 'package:lepig/widget/SearchView.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class Category extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _Category();
  }
}

class _Category extends State<Category> {
  List<SelectProductTypeModel> category = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getCategory();
    print("object");
  }

  bool isSelect = false;
  int selectIndex = 0;

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  getCategory() {
    HttpSelectProductType(context, 0).then((value) {
      if (value == null) {
        return;
      }
      selectIndex = value[0].id;
      category = value;
    });
  }

  @override
  Widget build(BuildContext context) {
    if(category.length<=0){
      return new SizedBox();
    }
    // TODO: implement build
    return new Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        elevation: 0,
        title: SearchView(),
      ),
      body: new Row(
        children: <Widget>[
          new Expanded(
            child: new SingleChildScrollView(
              child: new Container(
                width: 75.0,
                decoration: BoxDecoration(),
                child: new Column(
                    children: category.map((e) {
                  return _cateItem(
                    isSelect: e.id == selectIndex ? true : false,
                    selectProductTypeModel: e,
                  );
                }).toList()),
              ),
            ),
          ),
          category.length > 0
              ? new Container(
                  padding: EdgeInsets.all(10.0),
                  width: MediaQuery.of(context).size.width - 75,
                  child: _pageCate(category[0]))
              : new SizedBox(),
        ],
      ),
    );
  }


  //左菜单单个
  Widget _cateItem(
      {Function onTab,
      bool isSelect,
      SelectProductTypeModel selectProductTypeModel}) {

    return InkWell(
      child: new Container(
        decoration: BoxDecoration(
          color:
              isSelect ? Colors.transparent : Color.fromRGBO(247, 247, 247, 1),
          border: !isSelect
              ? Border(
                  bottom: BorderSide(color: Color.fromRGBO(233, 233, 233, 1)),
                  right: BorderSide(color: Color.fromRGBO(233, 233, 233, 1)),
                )
              : Border.all(color: Colors.transparent, width: 0),
        ),
        alignment: Alignment.center,
        height: 65.0,
        child: new Text(
          selectProductTypeModel.name,
          style: TextStyle(
            fontSize: 15.0,
          ),
        ),
      ),
      onTap: () {
        if(selectIndex== selectProductTypeModel.id){
          return ;
        }
        setState(() {
          selectIndex = selectProductTypeModel.id;
        });
        CategoryEntity entity = new CategoryEntity();
        entity.model = selectProductTypeModel;
        eventBus.fire(new CategoryEventEvent(entity));
      },
    );
  }
}

//分类页面
class _pageCate extends StatefulWidget {
  _pageCate(this.productTypeModel);

  SelectProductTypeModel productTypeModel;

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return new _pageCateState();
  }
}

class _pageCateState extends State<_pageCate> {
  StreamSubscription<CategoryEventEvent> _colorSubscription;
  SelectProductTypeModel productTypeModel;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    productTypeModel = widget.productTypeModel;
    print("productTypeModel");
    print(productTypeModel);
    // ignore: missing_return
    _colorSubscription = eventBus.on<CategoryEventEvent>().listen((event) {
      setState(() {
        productTypeModel = event.categoryEntity.model;
      });
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    if (_colorSubscription != null) {
      _colorSubscription.cancel();
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new PageView(
      onPageChanged: (int index) => {},
      scrollDirection: Axis.vertical,
      children: <Widget>[
        new SingleChildScrollView(
          child: new Column(
            children: <Widget>[
              new Text(productTypeModel.name),
              GridView(
                  shrinkWrap: true,
                  physics: NeverScrollableScrollPhysics(),
                  scrollDirection: Axis.vertical,
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      mainAxisSpacing: 20.0,
                      crossAxisSpacing: 20.0,
                      crossAxisCount: 3, //横轴三个子widget
                      childAspectRatio: 1.0 //宽高比为1时，子widget
                      ),
                  children: productTypeModel.child.map((e) {
                    return _pageCateItem(e.name, e.thum);
                  }).toList()),
            ],
          ),
        ),
      ],
    );
  }
}

//分类item
class _pageCateItem extends StatelessWidget {
  _pageCateItem(this.title, this.img);

  String title;
  String img;

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new InkWell(
      child: new Container(
        child: new Column(
          children: <Widget>[
            new ClipRRect(
              child: new Container(
                color: Colors.amber,
                child: new Image.network(
                  img,
                  fit: BoxFit.cover,
                  width: 60.0,
                ),
              ),
              borderRadius: BorderRadius.circular(80.0),
            ),
            new SizedBox(
              height: 5.0,
            ),
            new Text(
              title,
              style: TextStyle(color: Colors.black, fontSize: 13.0),
            )
          ],
        ),
        height: 150.0,
      ),
      onTap: (){
        SearchModel searchModel= new SearchModel();
        searchModel.isCate=true;
        searchModel.keyword=title;
        Navigator.of(context).pushNamed("/search",arguments: searchModel);
      },
    );
  }
}
